# -*- coding: utf-8 -*-
import os
import json

import platform
from tornado.options import options

from app.tools.pool import PgsqlPool, MySQLConnectionPool, ClickHouseConnectionPool


log_dir = os.path.join(os.path.dirname(__file__), '../logs/')
if not os.path.exists(log_dir):
    os.mkdir(log_dir)

log_file = os.path.join(os.path.dirname(__file__), '../logs/valkyrja.log')
if os.path.exists(log_file):
    os.remove(log_file)


options.logging = "info"
options.log_to_stderr = True
options.log_file_prefix = log_file
options.log_rotate_mode = "time"
options.log_file_max_size = 1024 * 1024 * 1024
options.log_file_num_backups = 10
options.log_rotate_when = 'D'
options.log_rotate_interval = 1


# path
root_path = os.path.dirname(__file__)
sys_type = platform.system()

if sys_type == 'Windows':
    root_path = root_path + '\\'
else:
    root_path = root_path + '/'

piece_path = root_path + 'static/tmp/'
if sys_type == 'Windows':
    piece_path = root_path + 'static\\tmp\\'


config_path = root_path + 'static/configure/'
if sys_type == 'Windows':
    config_path = root_path + 'static\\configure\\'

# config
file = os.path.join(root_path, "", config_path + "Default.json")
with open(file, "r", encoding="utf-8") as f:
    global_config = json.load(f)
# 读取kj83 安全监控配置文件
# sm_config_file = os.path.join(root_path, "", config_path + "SmConfig.json")
# with open(file, "r", encoding="utf-8") as f:
#     global_config['kj83_config'] = json.load(f)

# 调试模式: 开发者生产模式，debug=True
# 模板路径: template_path
# 静态文件路径: static_path
# 开启跨站伪装登录,xsrf_cookies=True
# 密钥,cookie_secret='uuid'
configs = dict(
    debug=False,
    autoreload=True,
    template_path=os.path.join(root_path, "templates"),
    static_path=os.path.join(root_path, "static"),
    xsrf_cookies=False,
    cookie_secret='a1ca2e68bd364596a1c307f804466261'
)


kj236_pool = None
# if global_config['name'] in ["KJ236", "OnePiece"]:
#     kj236_db = global_config['kj236_db']
#     kj236_pool = PgsqlPool(kj236_db) if kj236_db else None
mysql_db = {
    'user': global_config['db_user'],
    'host': global_config['db_ip'],
    'port': global_config['db_port'],
    'password': global_config['db_pwd'],
    'database': global_config['db_business_dbname']
}

staff_track_db = {
    'user': global_config['staff_track_db']['db_user'],
    'server_host': global_config['staff_track_db']['db_ip'],
    'port': global_config['staff_track_db']['db_port'],
    'password': global_config['staff_track_db']['db_pwd'],
    'db': global_config['staff_track_db']['db_dbname']
}
mysql_pool = MySQLConnectionPool(mysql_db)
clickhouse_pool = ClickHouseConnectionPool(staff_track_db)

if global_config['pieces']:
    piece_path = global_config['pieces']
